package cn.panda.edu.controller;

import cn.panda.edu.model.Teacher;
import cn.panda.edu.service.TeacherService;
import cn.panda.edu.util.Constant;
import cn.panda.edu.util.MathUtil;
import cn.panda.edu.util.ResultDataUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

@RestController
@RequestMapping(value="api/admin")
@Api(description="管理员登陆")
public class ManageLoginController {

	@Autowired
	private TeacherService teacherService;
	
	@ApiOperation(value ="管理员登陆", notes ="")
	@ApiImplicitParam(name ="teacher", value ="教师类", paramType ="form")
	@RequestMapping(value="/login",method=RequestMethod.POST)
	public Map<String, Object> manageLogin(Integer teacherId,String teacherPwd,HttpServletRequest request){
		String pwd = MathUtil.getMD5(teacherId.toString()+"#"+teacherPwd);
		System.out.println(teacherId+"---"+pwd);
		Teacher teacher2 = null;
			teacher2 = teacherService.manageLogin(teacherId,pwd);
		System.out.println(teacher2);
		if (teacher2 != null) {
			switch (teacher2.getRoleId()) {
			case 0:
				
				request.getSession().setAttribute(Constant.ADMIN_LOGIN_USER, teacher2);
				return ResultDataUtil.resData(200, "登陆成功！", teacher2);
			case 1:
				request.getSession().setAttribute(Constant.ADMIN_LOGIN_USER, teacher2);
				return ResultDataUtil.resData(200, "登陆成功！", teacher2);
			default:
				return ResultDataUtil.resData(500, "权限不足！", null);
			}
			
		}else {
			return ResultDataUtil.resData(500, "账号密码错误！", null);
		}
		
		
	}
	
	@ApiOperation(value ="管理员登出", notes ="")
	@RequestMapping(value="/logout",method=RequestMethod.POST)
	public Map<String, Object> manageLogin(HttpSession session){
		session.removeAttribute(Constant.ADMIN_LOGIN_USER);
		return ResultDataUtil.resData(200, "账号已退出！", null);
			
		
		
	}
	@ApiOperation(value ="密码验证", notes ="")
	@ApiImplicitParam(name="teacherPwd" ,value="教师密码", paramType="form")
	@RequestMapping(value="/queryterInfo",method=RequestMethod.POST)
	public Map<String, Object> queryterInfo(String teacherPwd,HttpSession session){
		Teacher teacher = (Teacher) session.getAttribute(Constant.ADMIN_LOGIN_USER);
		String pwd = MathUtil.getMD5(teacher.getTeacherId().toString()+"#"+teacherPwd);
		System.out.println(pwd+"-----"+teacher.getTeacherPassword()+"-----"+teacherPwd);
		if (pwd.equals(teacher.getTeacherPassword())) {
			return ResultDataUtil.resData(200, "密码正确！", null);
		}else {
			return ResultDataUtil.resData(500, "密码错误！", null);
		}
		
		
		
		
	}
}
